﻿using System;
using System.ComponentModel;
namespace TeacherLeftHand.Model
{
    public class KISearchCondition : ISearchCondition
    {
        #region constructor
        public KISearchCondition()
        {
            Keyword = null;
            MaxTopicNo = 0;
            MinTopicNo = 0;
            SelectedClassLevel = 0;
            Subject = null;
            AdvancedSearchMode = false;
        }
        #endregion

        #region properties

        private int _selectedClassLevel;
        private int _maxTopicNo;
        private int _minTopicNo;
        private string _keyword;
        private string _subject;

        /// <summary>
        /// The <see cref="AdvancedSearchMode" /> property's name.
        /// </summary>
        public const string AdvancedSearchModePropertyName = "AdvancedSearchMode";

        private bool _advancedSearchMode = false;

        /// <summary>
        /// Gets the AdvancedSearchMode property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public bool AdvancedSearchMode
        {
            get
            {
                return _advancedSearchMode;
            }

            set
            {
                if (_advancedSearchMode == value)
                {
                    return;
                }

                var oldValue = _advancedSearchMode;
                _advancedSearchMode = value;

                // Update bindings, no broadcast
                OnPropertyChanged(AdvancedSearchModePropertyName);
            }
        }

        public int SelectedClassLevel
        {
            get
            {
                return _selectedClassLevel;
            }
            set
            {
                if (_selectedClassLevel != value)
                {
                    _selectedClassLevel = value;
                    OnPropertyChanged("SelectedClassLevel");
                }
            }
        }

        public string Keyword
        {
            get
            {
                return _keyword;
            }
            set
            {
                if (_keyword != value)
                {
                    _keyword = value;
                    OnPropertyChanged("Keyword");
                }
            }
        }

        public string Subject
        {
            get
            {
                return _subject;
            }
            set
            {
                if (_subject != value)
                {
                    _subject = value;
                    OnPropertyChanged("Subject");
                }
            }
        }

        public int MinTopicNo
        {
            get
            {
                return _minTopicNo;
            }
            set
            {
                if (_minTopicNo != value)
                {
                    _minTopicNo = value;
                    if (value > _maxTopicNo)
                    {
                        MaxTopicNo = value;
                    }
                    OnPropertyChanged("MinTopicNo");
                }
            }
        }

        public int MaxTopicNo
        {
            get
            {
                return _maxTopicNo;
            }
            set
            {
                if (_maxTopicNo != value)
                {
                    _maxTopicNo = value;
                    if (value < _minTopicNo)
                        MinTopicNo = value;
                    OnPropertyChanged("MaxTopicNo");
                }
            }
        }

        /// <summary>
        /// The <see cref="Topic" /> property's name.
        /// </summary>
        public const string TopicPropertyName = "Topic";

        private string _topic = "";

        /// <summary>
        /// Gets the Topic property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public string Topic
        {
            get
            {
                return _topic;
            }

            set
            {
                if (_topic == value)
                {
                    return;
                }

                _topic = value;

                // Update bindings, no broadcast
                OnPropertyChanged(TopicPropertyName);
            }
        }
        #endregion

        #region Implementation of INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
    }
}
